package com.cola.utils.templateMessageUtils.utils;

import com.alibaba.fastjson.JSON;
import com.cola.enums.TemplateMessageType;
import com.cola.utils.PropertyUtils;
import com.cola.utils.templateMessageUtils.entity.Data;
import com.cola.utils.templateMessageUtils.entity.PlaceOrderSuccess.PlaceOrderSuccessMessage;
import com.cola.utils.templateMessageUtils.entity.PlaceOrderSuccess.PlaceOrderSuccessMessageData;
import com.cola.utils.templateMessageUtils.entity.newOrder.NewOrderMessage;
import com.cola.utils.templateMessageUtils.entity.newOrder.NewOrderMessageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Max on 2017/8/18.
 * 下单成功通知
 */
public class PlaceOrderSuccessMessageUtil {

    private static final Logger logger = LoggerFactory.getLogger(PlaceOrderSuccessMessageUtil.class);
    /**
     * 发送下单成功通知
     * @param orderNo
     * @param openId
     * @return
     */
    public static boolean send(String orderNo,String price,String openId,String orderTime){
        //获取消息实体json
        String messageJson = getMessageJson(orderNo,price,openId,orderTime);

        //插入待发送模板消息列表
        boolean status = TemplateCommon.insetWaitingSendMessage(orderNo, TemplateMessageType.PLACE_ORDER_SUCCESS.name(),openId,messageJson);

        if(status){
            logger.error("订单【"+orderNo+"】下单成功通知已成功插入待发送队列！");
        }else{
            logger.error("订单【"+orderNo+"】下单成功通知插入待发送队列失败！");
        }
        return status;
    }

    /**
     * 获取模板消息发送请求Json
     * @param orderNo
     * @param openId
     * @param orderTime
     * @return
     */
    private static String getMessageJson(String orderNo,String price,String openId,String orderTime){
        PlaceOrderSuccessMessageData messageData = new PlaceOrderSuccessMessageData();
        Data data = new Data();

        data.setValue(PropertyUtils.getProperty("place_order_message_title"));
        messageData.setFirst(data);

        data = new Data();
        data.setValue(orderNo);
        messageData.setKeyword1(data);

        data = new Data();
        data.setValue(price);
        messageData.setKeyword2(data);

        data = new Data();
        data.setValue("货到付款");
        messageData.setKeyword3(data);

        data = new Data();
        data.setValue(PropertyUtils.getProperty("place_order_message_remark"));
        messageData.setRemark(data);

        PlaceOrderSuccessMessage message = new PlaceOrderSuccessMessage();

        message.setData(messageData);
        message.setTemplate_id(PropertyUtils.getProperty("place_order_template_id"));

        message.setTouser(openId);

        message.setUrl(PropertyUtils.getProperty("place_order_url"));

        return JSON.toJSONString(message);
    }

    public static void main(String[] args) {
        send("NO111","200.00","obOPQwiBMxtetnUh5kx4DTctz2is","2017-08-18");
    }

}
